<?php


namespace app\admin\controller\mall;


use app\admin\model\MallProject;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;
use think\Model;

/**
 * Class Goods
 * @package app\admin\controller\mall
 * @ControllerAnnotation(title="商城商品管理")
 */
class Destruction extends AdminController
{

    use Curd;

    protected $relationSerach = true;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallProject();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $param = request()->param();
        if ($this->request->isAjax()) {
            //查找过期的数据  term  =2，3 （10年后过期，30后过期）
            $sql = "SELECT *,FROM_UNIXTIME(end_time) AS times FROM ea_archives_admin WHERE (DATE_ADD(FROM_UNIXTIME(end_time),INTERVAL 10 YEAR ) < NOW() AND term = 2) OR (DATE_ADD(FROM_UNIXTIME(end_time),INTERVAL 30 YEAR) < NOW() AND term=3) ORDER BY end_time ASC LIMIT " . $param['page'] . "," . $param['limit'];
            $sql2 ="SELECT count(*) as num FROM ea_archives_admin WHERE (DATE_ADD(FROM_UNIXTIME(end_time),INTERVAL 10 YEAR ) < NOW() AND term = 2) OR (DATE_ADD(FROM_UNIXTIME(end_time),INTERVAL 30 YEAR) < NOW() AND term=3)";
            $data = Db::query($sql);
            $count = Db::query($sql2);

            foreach($data as $k=>$v){
                $start = '';
                $end   = '';
                if($data[$k]['start_time']){
                    $start = date('Ymd',$data[$k]['start_time']);
                }
                if($data[$k]['end_time']){
                    $end  = date('Ymd',$data[$k]['end_time']);
                }
                $data[$k]['create_time'] = date('Y-m-d',$data[$k]['create_time']);


                $data[$k]['reference'] =$v['reference'].'-'.$v['key'];

                $data[$k]['start_end_date'] = $start .'-'.$end;

            }
            $data = [
                'code' => 0,
                'msg' => '',
                'count' => $count[0]['num'],
                'data' => $data,
            ];
            return json($data);
        }
        return $this->fetch();
    }


}